from django.shortcuts import render
from django.shortcuts import redirect
# Create your views here.
from django.shortcuts import HttpResponse


def login(request):
    print(request.method)  # 用于获取用户提交方式
    error_msg = ""
    if request.method == "POST":  # 表示这个状态是用户提交请求操作
        user = request.POST.get('user', None)
        pwd = request.POST.get('pwd', None)
        print(user, pwd)
        if user == '123' and pwd == '123':
            return redirect('/index')
        else:
            error_msg = "用户名用户密码不正确"
    return render(request, 'login.html', {'error_msg': error_msg})


USER_LIST = [
    {'username': 'alex', 'email': '163@com', 'gender': '男'}
]

for i in range(5):
    tmp = {'username': 'alex'+str(i), 'email': '163@com', 'gender': '男'}
    USER_LIST.append(tmp)


def index(request):
    if request.method == "POST":
        u = request.POST.get('username')
        e = request.POST.get('email')
        g = request.POST.get('gender')
        add_tmp = {'username': u, 'email': e, 'gender': g}
        USER_LIST.append(add_tmp)
    return render(request, 'index.html', {'user_list': USER_LIST})
